package com.sayimo.maker.user.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.user.dao.AreaInterfaceDao;
import com.sayimo.maker.user.dao.CityInterfaceDao;
import com.sayimo.maker.user.dao.CustomerInterfaceDao;
import com.sayimo.maker.user.dao.EnterpriseInfoDao;
import com.sayimo.maker.user.dao.EnterpriseWalletInterfaceDao;
import com.sayimo.maker.user.dao.ProviderRefDao;
import com.sayimo.maker.user.service.EnterpriseInfoService;
import com.sayimo.maker.user.vo.Area;
import com.sayimo.maker.user.vo.City;
import com.sayimo.maker.user.vo.Customer;
import com.sayimo.maker.user.vo.EnterpriseInfo;
import com.sayimo.maker.user.vo.EnterpriseWallet;
import com.sayimo.maker.user.vo.ProviderRef;

/**
 * @Title: EnterpriseInfoServiceImpl.java
 * @Package com.sayimo.maker.base.service.impl
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 田超
 * @date 2017年2月18日 上午10:02:20
 * @version V1.0
 */
@Service("enterpriseInfoService")
public class EnterpriseInfoServiceImpl implements EnterpriseInfoService {

	private static Log logger = LogFactory.getLog(EnterpriseInfoServiceImpl.class);

	@Resource(name = "enterpriseInfoDao")
	private EnterpriseInfoDao enterpriseInfoDao;
	@Resource(name = "customerInterfaceImplDao")
	private CustomerInterfaceDao customerInterfaceImplDao;
	@Resource(name = "providerRefDao")
	private ProviderRefDao providerRefDao;
	@Autowired
	private CityInterfaceDao cityInterfaceImplDao;
	@Autowired
	private AreaInterfaceDao areaInterfaceImplDao;
	@Resource(name = "enterpriseWalletInterfaceDao")
	private EnterpriseWalletInterfaceDao enterpriseWalletInterfaceDao;

	/**
	 * 
	 * @Title: insertProviderAndUnion
	 * @Author: 田超
	 * @Date:日期：2017年2月18日 : 下午3:07:43
	 * @Description:供应商和联盟商入驻
	 * @return Map<String,Object>
	 * @Version 1.0.0
	 */
	public Map<String, Object> insertProviderAndUnion(HttpServletRequest request) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Integer id = (Integer) request.getAttribute("customerId"); // customerId
			String accout = (String) request.getAttribute("accout"); // 账号
			String photoUrl = request.getParameter("photourl"); // logo地址
			String providerName = request.getParameter("providername");
			String linkman = request.getParameter("linkman"); // 联系人
			String mobilePhone = request.getParameter("mobilephone"); // 联系手机
			String industry = request.getParameter("industry"); // 所属行业
			String industryCode = request.getParameter("industrycode");
			String corporate = request.getParameter("corporate"); // 法人
			String lockPhone = request.getParameter("lockphone"); // 法人联系方式
			String creditCode = request.getParameter("creditcode"); // 营业执照编号
			String crePhotoUrl = request.getParameter("crephotourl"); // 营业执照地址
			String areaCode = request.getParameter("areaCode"); // 所在地区编号
			String address = request.getParameter("address"); // 详细地址
			Integer type = Integer.parseInt(request.getParameter("type")); // 1.供应商
																			// 2.联盟商
			String _serviceCharge = request.getParameter("serviceCharge"); // 联盟商认证平台服务费
			String description = request.getParameter("description"); // 供应商介绍
			String product = request.getParameter("product"); // 主营产品
			String unionshopClassId = request.getParameter("unionshopClassId"); // 联盟商店铺分类1餐饮美食，2酒店住宿，3休息娱乐，4美容美发，5其他
			String bankCodeNo = request.getParameter("bankCodeNo");
			String bankBranchCode= request.getParameter("bankBranchCode");
			String bankBranchName = request.getParameter("bankBranchName");
			Integer bindType = Integer.parseInt(request.getParameter("bindType"));
			String bankBindUserName = request.getParameter("bankBindUserName");
			EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
			enterpriseInfo.setCustomerId(id);
			enterpriseInfo.setProviderName(providerName);
			enterpriseInfo.setProviderType(type);
			enterpriseInfo.setPhotoUrl(photoUrl);
			enterpriseInfo.setLinkman(linkman);
			enterpriseInfo.setMobilePhone(mobilePhone);
			enterpriseInfo.setIndustry(industry);
			enterpriseInfo.setIndustryCode(industryCode);
			enterpriseInfo.setCorporate(corporate);
			enterpriseInfo.setLockPhone(lockPhone);
			enterpriseInfo.setCreditCode(creditCode);
			enterpriseInfo.setCrePhotoUrl(crePhotoUrl);

			enterpriseInfo.setAreaCode(areaCode);
			enterpriseInfo.setAddress(address);
			enterpriseInfo.setDescription(description);
			enterpriseInfo.setProduct(product);
			if (unionshopClassId != null && !"".equals(unionshopClassId)) {
				enterpriseInfo.setUnionshopClassId(Integer.valueOf(unionshopClassId));
			}

			Area area = areaInterfaceImplDao.getAreaByAreaCode(areaCode);
			if (area != null) {
				City city = cityInterfaceImplDao.getCityByCityCode(area.getCityCode());
				enterpriseInfo.setProvinceCode(city.getProvinceCode());
				enterpriseInfo.setCityCode(area.getCityCode());
			}

			enterpriseInfo.setCreateUser(0);
			if (type == 2) {
				Integer serviceCharge = Integer.parseInt(_serviceCharge);
				enterpriseInfo.setServiceCharge(serviceCharge);
			}

			Integer count = 1;
			while (count != 0) {
				// 生成供应商编号
				String providerCode = RandomStringUtils.random(3, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
				String code = RandomStringUtils.random(2, "0123456789");
				providerCode = providerCode + code;
				enterpriseInfo.setProviderCode(providerCode);
				// 判断是否已存在该供应商编号
				count = enterpriseInfoDao.getIsProviderCode(providerCode);
			}

			Integer entNum = enterpriseInfoDao.insertEnterpriseInfo(enterpriseInfo);

			ProviderRef provider = new ProviderRef();
			provider.setProviderType(type);
			provider.setRefId(enterpriseInfo.getId());
			Integer providerNum = providerRefDao.insertProviderRef(provider);
			Customer customer = new Customer();
			customer.setAccout(accout);
			customer.setProviderRefId(provider.getId());
			Integer customNum = customerInterfaceImplDao.updateCustomer(customer);
			// 插入供应商钱包信息
			EnterpriseWallet wallet = new EnterpriseWallet();
			wallet.setProviderRefId(provider.getId());
			wallet.setBindType(bindType);
			wallet.setBankBranchName(bankBranchName);
			wallet.setBankBranchCode(bankBranchCode);
			wallet.setBankCodeNo(bankCodeNo);
			wallet.setBankBindUserName(bankBindUserName);
			wallet.setStatus(1);
			Integer walletNum = enterpriseWalletInterfaceDao.insertWallet(wallet);
			walletNum = enterpriseWalletInterfaceDao.updateWallet(wallet);
			if (entNum > 0 && providerNum > 0 && customNum > 0 && walletNum > 0) {
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			} else {
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
			}
		} catch (DaoException e) {
			logger.info(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}

	/**
	 * 
	 * @Title: insertProviderAndUnion
	 * @Author: 龚益君
	 * @Date:日期：2017年3月2日 : 下午3:07:43
	 * @Description:根据用户账号查找用户的商户信息
	 * @return Map<String,Object>
	 * @Version 1.0.0
	 */
	@Override
	public Map<String, Object> queryEnterpriseInfo(String account) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isEmpty(account)) {
				logger.error("params error");
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_REQUEST_PARAM_ERROR);
				return resultMap;
			}
			Map<String, Object> enterInfoMap = new HashMap<String, Object>();
			EnterpriseInfo enterInfo = enterpriseInfoDao.getEnterpriseInfoByAccount(account);
			if (enterInfo != null) {
				enterInfoMap.put("enterpriseInfo", enterInfo);
			}
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, enterInfoMap);
		} catch (DaoException e) {
			logger.info(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.maker.user.service.impl
	 * @Author 谭新
	 * @MethodName: updateProviderAndUnion
	 * @Description:修改供应商联盟商
	 * @Date 2017年3月14日,上午9:20:15
	 */
	public Map<String, Object> updateProviderAndUnion(EnterpriseInfo enterpriseInfo)
			throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Area area = areaInterfaceImplDao.getAreaByAreaCode(enterpriseInfo.getAreaCode());
			if (area != null) {
				City city = cityInterfaceImplDao.getCityByCityCode(area.getCityCode());
				enterpriseInfo.setProvinceCode(city.getProvinceCode());
				enterpriseInfo.setCityCode(area.getCityCode());
			}
			enterpriseInfo.setUpdateUser(0);
			enterpriseInfo.setStatus(1);
			Integer entNum = enterpriseInfoDao.updateEnterpriseInfo(enterpriseInfo);			
			if (entNum > 0) {
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			} else {
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
			}
		} catch (DaoException e) {
			logger.info(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}
}
